/*
 * Copyright (C) 2008 Chris Bellew
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "iDynDNSController.h"
#import "iDynDNSSettingsView.h"
#import <UIKit/UIView.h>
#import <UIKit/UIHardware.h>


static iDynDNSController *_sharedInstance = nil;


@implementation iDynDNSController

+ (iDynDNSController *)sharedInstance
{
	if(!_sharedInstance) {
		_sharedInstance = [[iDynDNSController alloc] init];
	}
    return _sharedInstance;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
{	
	_sharedInstance = self;
	
	
    struct CGRect rect;
	rect = [UIHardware fullScreenApplicationContentRect];
    rect.origin.x = rect.origin.y = 0;

	_mainWindow = [[UIWindow alloc] initWithContentRect:rect];

	_mainView = [[UITransitionView alloc] initWithFrame:rect];
	
	[_mainWindow orderFront:self];
    [_mainWindow makeKey:self];
	[_mainWindow _setHidden: NO];
    [_mainWindow setContentView:_mainView];
	

	[self showView:[iDynDNSSettingsView sharedInstance] event:2];
	
}


-(UIView*)mainView {
	return _mainView;	
}

- (id)showView:(UIView*)view event: (int) eventNum{
	[_mainView transition:eventNum toView:view];
	return self;
}


-(void)dealloc {
	[_mainWindow release];
	[_mainView release];
	[[iDynDNSSettingsView sharedInstance] release];
    [super dealloc];
}

- (void)applicationWillTerminate;
{
	NSLog(@"terminating");
}

- (UIWindow*)mainWindow {
	return _mainWindow;
	
}

- (NSString *)executeCommand:(NSString *)cmd
{
    NSString *output = [NSString string];
    FILE *pipe = popen([cmd cStringUsingEncoding: NSASCIIStringEncoding], "r");
    if (!pipe) return;

    char buf[1024];
    while(fgets(buf, 1024, pipe)) {
        output = [output stringByAppendingFormat: @"%s", buf];
    }

    pclose(pipe);
    return output;
}

- (void)openProgressHUD:(NSString*)message
{
    _refreshProgressHUD = [[UIProgressHUD alloc] initWithWindow:_mainWindow];
    [_refreshProgressHUD setText:message];
    [_refreshProgressHUD setFontSize:12];
    [_refreshProgressHUD show:YES];
    [_mainView addSubview:_refreshProgressHUD];
}

- (void)closeProgressHUD
{
    if ( _refreshProgressHUD ) {
        [_refreshProgressHUD show:NO];
        [_refreshProgressHUD removeFromSuperview];
        [_refreshProgressHUD release];
    }
}


@end

